package demo01;

public class Chengxu30 {
    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 7, 9};
        int num = 6;
        int[] newArr = insertNum(arr, num);
        for (int i : newArr) {
            System.out.print(i + " ");
        }
    }

    public static int[] insertNum(int[] arr, int num) {
        int[] newArr = new int[arr.length + 1];
        boolean inserted = false;
        for (int i = 0, j = 0; i < arr.length; i++, j++) {
            if (!inserted && num <= arr[i]) {
                newArr[j] = num;
                j++;
                inserted = true;
            }
            newArr[j] = arr[i];
        }
        if (!inserted) {
            newArr[newArr.length - 1] = num;
        }
        return newArr;
    }
}

